package cn.isjinhao.crws.sql.lexical;

import cn.isjinhao.crws.sql.lexical.scan.CharSequence;
import cn.isjinhao.crws.sql.lexical.scan.TokenSequence;
import cn.isjinhao.crws.sql.lexical.token.KeyWords;
import cn.isjinhao.crws.sql.lexical.token.Token;
import cn.isjinhao.crws.sql.lexical.token.TokenType;
import cn.isjinhao.crws.sql.lexical.token.TokenTypeLexemeMapping;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author ISJINHAO
 * @Date 2021/2/23 19:58
 */
public abstract class Scanner {

    protected TokenSequence tokenSequence;

    protected KeyWords keyWords = KeyWords.getInstance();

    private Set<String> functionNameSet = new HashSet<>();

    protected CharSequence charSequence;

    public Scanner(String source) {
        charSequence = new CharSequence(source);
        tokenSequence = new TokenSequence();
    }

    public TokenSequence scanTokens() {
        while (!charSequence.isAtEnd()) {
            charSequence.recordLast();
            scanToken();
        }
        tokenSequence.addItem(new Token(TokenType.EOF, null));
        return tokenSequence;
    }

    /**
     * 扫描 Token
     */
    abstract protected void scanToken();

    protected void addToken(TokenType type) {
        addToken(type, TokenTypeLexemeMapping.getLexeme(type));
    }

    protected void addToken(TokenType type, Object literal) {
        tokenSequence.addItem(new Token(type, literal));
    }

}
